<?php
    global $_W,$_GPC;
    load() -> func('tpl');
    $ops = array('display', 'edit', 'delete'); // 只支持此 3 种操作.
    $op = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'display';

    if($op == 'display'){
        $id = intval($_GPC['oid']);

        if(empty($id)){
            message('未找到指定运单');
        }

        $order = m('order')->getOrdersById($id);

        $orderId = $order['orderId'];

        if(!$orderId){
            message('系统出错');
        }

        $orderDetail = m('detail')->getOrderDetailByOrderId($orderId);

        include $this->template('order_detail');
    }

    //跟踪地点编辑
    if ($op == 'edit') {
        $oid = intval($_GPC['oid']);

        $id = intval($_GPC['id']);
        if(!empty($id)){
            $detail = m('detail')->getOrderDetailById($id);
            if(empty($detail)){
                message('未找到指定的跟踪地点信息.', $this->createWebUrl('goods') , 'error');
            }
        }
        if (checksubmit()) {
            $order = m('order')->getOrdersById($oid);
            $orderId = $order['orderId'];
            $data = $_GPC['detail'];
            $data['orderId'] = $orderId;
            $data['createdAt'] = strtotime($data['createdAt']);
            if(empty($detail)){
                $data['uniacid'] = $_W['uniacid'];
                $data['updatedAt'] = TIMESTAMP;
                $ret = pdo_insert('ew_delivery_order_detail', $data);
            } else {
                $data['updatedAt'] = TIMESTAMP;
                $ret = pdo_update('ew_delivery_order_detail', $data, array('id'=>$id));
            }

            message('跟踪地点信息保存成功', $this->createWebUrl('detail' , array('oid'=>$oid , 'op' => 'display')), 'success');
        }
        include $this->template('detail_edit');
    }

    if($op == 'delete') {
        $id = intval($_GPC['id']);

        if(empty($id)){
            message('未找到指定运单信息');
        }
        $result = pdo_delete('ew_delivery_order_detail', array('id'=>$id, 'uniacid'=>$_W['uniacid']));
        if($result){
            echo json_encode(array('status' => 1 , 'msg' => '删除成功'));
        } else {
            echo json_encode(array('status' => 0 , 'msg' => '删除失败'));
        }
    }
?>